<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="addRules">增加停车计费规则</el-button>
      <el-button @click="exportToExcel">导出Excel</el-button>
    </div>
    <!-- 新增弹框 -->
    <el-dialog :visible.sync="dialogVisible" width="680px" :title="ruleId?'编辑规则':'新增规则'">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="top">
          <el-form-item label="计费规则编号" prop="ruleNumber">
            <el-input v-model="addForm.ruleNumber" />
          </el-form-item>
          <el-form-item label="计费规则名称" prop="ruleName">
            <el-input v-model="addForm.ruleName" />
          </el-form-item>
          <el-form-item label="计费方式" prop="chargeType">
            <el-radio-group v-model="addForm.chargeType" size="small">
              <el-radio label="duration" border>时长收费</el-radio>
              <el-radio label="turn" border>按次收费</el-radio>
              <el-radio label="partition" border>分段收费</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="flex-container rules" style="display:flex">
            <el-form-item label="免费时长">
              <el-input-number
                v-model="addForm.freeDuration"
                controls-position="right"
                :min="1"
              />
              分钟
            </el-form-item>
            <el-form-item label="收费上限">
              <el-input v-model="addForm.chargeCeiling" style="width:120px" />
              元
            </el-form-item>
          </div>
          <!-- == 根据计费方式切换计费规则表单项 == -->
          <el-form-item label="计费规则" class="rules" required>
            <!-- 按时长收费区域 -->
            <div v-if="addForm.chargeType==='duration'" class="duration" style="display:flex">
              每
              <el-form-item prop="durationTime">
                <el-input-number
                  v-model="addForm.durationTime"
                  controls-position="right"
                  :min="1"
                />
              </el-form-item>
              小时
              <el-form-item prop="durationPrice">
                <el-input v-model="addForm.durationPrice" class="input-box" />
              </el-form-item>
              元
            </div>
            <!-- 按次收费区域 -->
            <div v-if="addForm.chargeType==='turn'" class="turn" style="display:flex">
              每次
              <el-form-item prop="turnPrice">
                <el-input v-model="addForm.turnPrice" class="input-box" />
              </el-form-item>
              元
            </div>
            <!-- 按分段收费区域 -->
            <div v-if="addForm.chargeType==='partition'" class="partition">
              <div class="item" style="display:flex">
                <el-form-item prop="partitionFrameTime">
                  <el-input-number
                    v-model="addForm.partitionFrameTime"
                    controls-position="right"
                    :min="1"
                  />
                </el-form-item>
                小时内,每小时收费
                <el-form-item prop="partitionFramePrice">
                  <el-input v-model="addForm.partitionFramePrice" class="input-box" style="width:100px" />
                </el-form-item>
                元
              </div>
              <div class="item" style="display:flex;margin:30px auto 0">
                每增加
                <el-form-item prop="partitionIncreaseTime">
                  <el-input-number
                    v-model="addForm.partitionIncreaseTime"
                    controls-position="right"
                    :min="1"
                  />
                </el-form-item>
                小时,增加
                <el-form-item prop="partitionIncreasePrice">
                  <el-input v-model="addForm.partitionIncreasePrice" class="input-box" style="width:120px;" />
                </el-form-item>
                元
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addConfirmFn">确 定</el-button>
      </template>
    </el-dialog>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column
          v-for="(item,index) in formList"
          :key="index + 1"
          :prop="Object.keys(item)[0]"
          :label="Object.values(item)[0]"
        />
        <el-table-column
          label="计费方式"
        >
          <template #default="scope">
            {{ chargeTypeObj[scope.row.chargeType] }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scoup">
            <el-button size="mini" type="text" @click="editRules(scoup.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delRules(scoup.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-container">
        <el-pagination
          layout="total, prev, pager, next,sizes"
          :page-sizes="[5,10,20,30,40,50]"
          :total="total"
          :current-page.sync="currentpage"
          :page-size="params.pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getRuleListAPI, addRuleAPI, deleteRuleAPI, getRuleDetailAPI, editRuleAPI } from '@/apis/car'
import { utils, writeFileXLSX } from 'xlsx'
export default {
  name: 'Building',
  data() {
    return {
      formList: [
        { 'ruleNumber': '计费规则编号' },
        { 'ruleName': '计费规则名称' },
        { 'freeDuration': '免费时长（分钟）' },
        { 'chargeCeiling': '收费上线（元）' },
        { 'chargeType': '计费方式' },
        { 'ruleNameView': '计费规则' }
      ],
      formData: {
        'id': '序号',
        'ruleNumber': '计费规则编号',
        'ruleName': '计费规则名称',
        'freeDuration': '免费时长（分钟）',
        'chargeCeiling': '收费上线（元）',
        'chargeType': '计费方式',
        'ruleNameView': '计费规则'
      },
      ruleList: [],
      params: {
        page: 1,
        pageSize: 10
      },
      total: 0,
      currentpage: 1, // 当前页
      chargeTypeObj: {
        duration: '时长收费',
        turn: '按次收费',
        partition: '分段收费'
      },
      dialogVisible: false, // 控制弹框关闭打开
      addForm: {
        ruleNumber: '', // 计费规则编号
        ruleName: '', // 计费规则名称
        chargeType: 'duration', // 计费规则类型 duration / turn / partition
        chargeCeiling: null,
        freeDuration: null,
        // 时长计费独有字段
        durationTime: null, // 时长计费单位时间
        durationPrice: null, // 时长计费单位价格
        durationType: 'hour',
        // 按次收费独有字段
        turnPrice: null,
        // 分段计费独有字段
        partitionFrameTime: null, // 段内时间
        partitionFramePrice: null, // 段内费用
        partitionIncreaseTime: null, // 超出时间
        partitionIncreasePrice: null // 超出费为收费价钱

      }, // 计费规则表单对象
      // 计费规则校验规则对象
      addFormRules: {
        ruleNumber: [
          {
            required: true,
            message: '请输入规则编号',
            trigger: 'blur'
          }
        ],
        ruleName: [
          {
            required: true,
            message: '请输入规则名称',
            trigger: 'blur'
          }
        ],
        chargeType: [
          {
            required: true,
            message: '请选择收费类型',
            trigger: 'blur'
          }
        ]
      },
      // 计费规则id
      ruleId: null
    }
  },
  created() {
    this.getRuleList()
  },
  methods: {
    // 获取数据
    async getRuleList() {
      const res = await getRuleListAPI(this.params)
      this.ruleList = res.data.rows
      this.total = res.data.total
    },
    // formatterFn(row) {
    //   const keyObj = {
    //     duration: '时长收费', turn: '按次收费', partition: '分段收费'
    //   }
    //   return keyObj[row.chargeType]
    // },
    // 分页下拉列表
    handleSizeChange(val) {
      this.params.pageSize = val
      this.currentpage = 1
      this.handleCurrentChange()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getRuleList()
    },
    // 获取计费规则
    exportToExcel() {
      // 目标：解决表格每行数据的先后顺序
      // 顺序取决于对象属性的 Object.keys() 拿出来的顺序
      // 知识：Object.keys() 按照对象属性添加的先后顺序获取成属性集合数组,但是如果属性有数字则会放到前面按照值大小顺序收集（不要让对象有数字属性）
      // 思路：把后台给我的数组对象，按照我组织的key的先后顺序，往空白对象里添加（插件内的方法 Object.keys() 取对象属性时就能按照我插入的先后顺序来获取赋予表格里的值）
      const keys = ['id', 'ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      const newDataList = this.ruleList.map((item, index) => {
        const newObj = {}
        keys.forEach(keyStr => {
          if (keyStr === 'chargeType') {
            newObj[keyStr] = this.chargeTypeObj[item[keyStr]]
          } else if (keyStr === 'id') {
            newObj[keyStr] = index + 1
          } else {
            newObj[keyStr] = item[keyStr]
          }
        })
        return newObj
      })
      // 1. 创建一个新的工作簿
      const workbook = utils.book_new()
      // 2. 创建一个工作表 要求一个对象数组格式（可以根据需要创建多个）
      const worksheet = utils.json_to_sheet(newDataList)
      // 3. 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, '停车计费规则')
      // 改写表头
      // 对象字段属性数组 => 中文字符串数组
      // 遍历 keys 数组  顺序遍历，映射的中文名肯定按照顺序的
      const headerList = keys.map(item => this.formData[item])
      utils.sheet_add_aoa(worksheet, [headerList], { origin: 'A1' })
      // 4. 导出方法进行导出
      writeFileXLSX(workbook, '行车管理-停车计费规则.xlsx')
    },
    // 新增或编辑规则弹窗
    addRules() {
      this.dialogVisible = true
    },
    // 新增规则
    async addConfirmFn() {
      if (this.ruleId) {
        const obj = {
          id: this.ruleId,
          ...this.addForm
        }
        await editRuleAPI(obj)
        this.$message.success('编辑成功')
      } else {
        await addRuleAPI(this.addForm)
        this.$message.success('新增成功')
      }
      this.dialogVisible = false
      this.getRuleList()
    },
    // 删除规则
    async delRules(id) {
      const res = await deleteRuleAPI(id)
      console.log(res)
      this.$message.success('删除成功')
      this.getRuleList()
    },
    // 编辑规则
    async editRules(id) {
      this.dialogVisible = true
      this.ruleId = id
      const res = await getRuleDetailAPI(id)
      console.log(res)
      this.addForm = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.rules .el-form-item {
  padding:0 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
